package com.luania.todolist2.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ListView;

import com.luania.todolist2.Adapter.TaskAdapter;
import com.luania.todolist2.Model.TaskModel;
import com.luania.todolist2.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by tyz1201 on 2016/4/1.
 */
public class CotainsActivity extends AppCompatActivity {
    private String groupName;
    ListView lv_contains;
    Intent intent;
    List<TaskModel> images = new ArrayList<>();
    Map<String, String> map = new HashMap<>();
    private String task_name, tasks;
    TaskAdapter taskAdapter;
    Boolean isNeedNotify;
    String notifyTime;
    Long currentTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cotains);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar3);

        intent = getIntent();
        groupName = intent.getStringExtra("groupName");

        toolbar.setTitle(groupName);
        toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
        setSupportActionBar(toolbar);

        lv_contains = (ListView) findViewById(R.id.lv_cotains);
        initImages();
        sortImages();
        taskAdapter = new TaskAdapter(images, CotainsActivity.this);
        lv_contains.setAdapter(taskAdapter);

    }

    @Override
    protected void onResume() {
        super.onResume();

        images.clear();
        initImages();
        sortImages();

        taskAdapter.notifyDataSetChanged();

    }

    private void initImages() {
        SharedPreferences sharedPreferences = getSharedPreferences("task_name", MODE_PRIVATE);
        map = (Map) sharedPreferences.getAll();

        Set<String> keySet = map.keySet();
        for (String key : map.keySet()) {
            task_name = key;
            tasks = map.get(key);

            String[] strs;
            strs = tasks.split("/");
            isNeedNotify = Boolean.parseBoolean(strs[2]);
            notifyTime = strs[0];
            currentTime = Long.parseLong(strs[3]);

            TaskModel taskModel = new TaskModel(notifyTime, task_name, isNeedNotify, currentTime);

            images.add(taskModel);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        groupName = intent.getStringExtra("groupName");
        if (id == R.id.action_add_group) {
            Intent intentNew = new Intent(CotainsActivity.this, TaskActivity.class);
            intentNew.putExtra("taskName", groupName);
            startActivity(intentNew);
        }
        return super.onOptionsItemSelected(item);
    }

    private void sortImages() {
        for (int i = 0; i < images.size(); i++) {
            Log.d("error", "i:" + i);
            TaskModel taskModel, taskModel1, midTaskModel;
            Long ctTime, ctTime1;

            for (int j = i + 1; j < images.size(); j++) {
                Log.d("error", "j:" + j);
                taskModel = images.get(i);
                taskModel1 = images.get(j);

                ctTime = taskModel.getCurrentTime();
                ctTime1 = taskModel1.getCurrentTime();
                if (ctTime > ctTime1) {
                    midTaskModel = images.get(i);
                    images.set(i,images.get(j));
                    images.set(j,midTaskModel);
//                    midTaskModel = taskModel1;
//                    taskModel1 = taskModel;
//                    taskModel = midTaskModel;
                }
//                images.add(taskModel);
            }
        }

    }
}

